import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import Home from '../views/Home.vue'
import Login from '../views/login/Login.vue'
import Register from '../views/login/Register.vue';
import ResetPassword from '../views/login/ResetPassword.vue';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    meta: {
      title: "首页",
      requireAuth: true
    },
    component: () => import('../views/Home')
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录'
    },
    component: () => import('../views/login/Login')
  },
  {
    path: '/register',
    name: 'Register',
    meta:{
      title: '注册'
    },
    component: () => import('../views/login/Register')
  },
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    meta:{
      title: '重置密码'
    },
    component: () => import('../views/login/ResetPassword')
  },
  {
    path: '/editPassword',
    name: 'EditPassword',
    meta:{
      title: '修改密码'
    },
    component: () => import('../views/login/EditPassword')
  },
  {
    path:'/order/index',
    name:'OrderIndex',
    meta:{
      title: "订单",
      requireAuth: true
    },
    component: () => import('@/views/order/Index')
  },
  {
    path:'/order/detail',
    name:'OrderDetail',
    meta:{
      title: "订单详情",
      requireAuth: true
    },
    component: () => import('@/views/order/Detail')
  },
  {
    path:'/order/upload',
    name:'OrderUpload',
    meta:{
      title: "病历夹",
      requireAuth: true
    },
    component: () => import('@/views/order/Upload')
  },
  {
    path:'/order/plan',
    name:'OrderPlan',
    meta:{
      title: "服务策划",
      requireAuth: true
    },
    component: () => import('@/views/order/Plan')
  },
  {
    path:'/order/summary',
    name:'OrderSummary',
    meta:{
      title: "服务总结",
      requireAuth: true
    },
    component: () => import('@/views/order/Summary')
  },
  {
    path:'/user/index',
    name:'UserIndex',
    meta:{
      title: "个人中心",
      requireAuth: true
    },
    component: () => import('@/views/user/Index')
  },
  {
    path:'/common/agreement',
    name:'Agreement',
    meta:{
      title: "服务协议"
    },
    component: () => import('@/views/common/Agreement')
  },
  {
    path:'/common/about',
    name:'About',
    meta:{
      title: "关于我们"
    },
    component: () => import('@/views/common/About')
  },
  {
    path:'/common/article',
    name:'Article',
    meta:{
      title: "QUICK"
    },
    component: () => import('@/views/common/Article')
  },
  {
    path:'/user/card',
    name:'Card',
    meta:{
      title: "银行卡",
      requireAuth: true
    },
    component: () => import('@/views/user/Card')
  },
  {
    path:'/user/cardAdd',
    name:'CardAdd',
    meta:{
      title: "添加银行卡支付宝",
      requireAuth: true
    },
    component: () => import('@/views/user/CardAdd')
  },
  {
    path:'/user/infos',
    name:'Infos',
    meta:{
      title: "个人资料",
      requireAuth: true
    },
    component: () => import('@/views/user/Infos')
  },
  {
    path:'/user/CertAuth',
    name:'CertAuth',
    meta:{
      title: "个人认证",
      requireAuth: true
    },
    component: () => import('@/views/user/CertAuth')
  },
]

const router = new VueRouter({
  routes
})

/**
 * 全局前置路由守卫
 */
router.beforeEach(async (to, from, next) => {
  let userInfo = JSON.parse(localStorage.getItem('userInfo'));

  if (to.meta.requireAuth) {
    if(userInfo) {
      if(store.getters.userInfo){
        next();
      }else{
        store.commit('setUserInfo', userInfo);
        next();
      }
    } else {
      next('/login');
    }
  } else {
    if(userInfo && store.getters.userInfo && to.path === '/login'){
      next('/');
    }else{
      next();
    }
  }
});

export default router
